<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>批量导入成绩</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../css/bootstrap/bootstrap.min.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="../css/font-awesome/font-awesome.min.css">
    <!-- Ionicons -->
    <link rel="stylesheet" href="../css/ionicons/ionicons.min.css">
    <!-- Theme style -->
    <link rel="stylesheet" href="../css/AdminLTE/AdminLTE.min.css">
    <!-- AdminLTE Skins. We have chosen the skin-blue for this starter
          page. However, you can choose any other skin. Make sure you
          apply the skin class to the body tag so the changes take effect. -->
    <link rel="stylesheet" href="../css/skins/skin-blue.min.css">
    <link rel="stylesheet" href="../css/fileinput/fileinput.min.css">

    <link rel="stylesheet" href="../css/bootstrap/bootstrap-editable.css">
    <link rel="stylesheet" href="../css/bootstrap/bootstrap-table.min.css">
    <link rel="stylesheet" href="../css/bootstrap/bootstrap-table-fixed-columns.min.css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="../js/plugins/browserhacks/html5shiv.min.js"></script>
    <script src="../js/plugins/browserhacks/respond.min.js"></script>
    <![endif]-->

    <!-- jQuery 3 -->
    <script src="../js/plugins/jquery/jquery.min.js"></script>
    <!-- Bootstrap 3.3.7 -->
    <script src="../js/plugins/bootstrap/bootstrap.min.js"></script>
    <!-- AdminLTE App -->
    <script src="../js/plugins/adminlte/adminlte.min.js"></script>

    <script src="../js/plugins/bootstrap/tableExport.min.js"></script>

    <script src="../js/plugins/bootstrap/bootstrap3-editable.min.js"></script>
    <script src="../js/plugins/bootstrap/bootstrap-table.min.js"></script>
    <script src="../js/plugins/bootstrap/bootstrap-table-fixed-columns.min.js"></script>
    <script src="../js/plugins/bootstrap/bootstrap-table-zh-CN.min.js"></script>
    <script src="../js/plugins/bootstrap/bootstrap-table-export.min.js"></script>
    <script src="../js/plugins/bootstrap/bootstrap-table-editable.min.js"></script>

    <!-- 文件上传插件-->
    <script src="../js/plugins/fileinput/fileinput.min.js"></script>
    <script src="../js/plugins/fileinput/zh.js"></script>
    <script src="../js/plugins/fileinput/theme.js"></script>

    <style>
        table{
            font-size:12px;

        }
        table th,table td{
            padding:0.25rem !important;
        }
    </style>

</head>
<body ng-app="myApp" ng-controller="filemyController">
<div class="container-fluid">
    <form class="form-horizontal">
        <div class="form-group" style="margin-top:20px">
            <label for="location" class="col-sm-2 control-label">选择文件</label>
            <div class="col-sm-8">
                <div class="input-group">
                    <input id='location' class="form-control" onclick="$('#i-file').click();">
                    <label class="input-group-btn">
                        <input type="button" id="i-check" value="浏览文件" class="btn btn-primary"
                               onclick="$('#i-file').click();">
                    </label>
                </div>
            </div>
            <input type="file" name="file" id='i-file'  accept=".xls, .xlsx" onchange="angular.element(this).scope().readMyFile(this);" style="display: none">
        </div>
     </form>
</div>
<div class="container-fluid">

    <div class="row">
        <div class="col-sm-12">

        </div>
    </div>


</div>
<div class="container-fluid">
    <div class="row">
        <div class="col-sm-12">
            <div>

                <!-- Nav tabs -->
                <ul class="nav nav-tabs" role="tablist">
                    <li role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">统计数据</a></li>
                    <li role="presentation"><a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">各科成绩</a></li>
                   </ul>

                <!-- Tab panes -->
                <div class="tab-content">
                    <div role="tabpanel" class="tab-pane active" id="home">
                        <div id="toolbar">

                        </div>
                        <table
                                id="table"
                                data-detail-view="true"
                                data-show-toggle="true"
                                data-search="true"
                                data-toggle="table"
                                data-toolbar="#toolbar"
                                data-show-columns="true"
                                data-click-to-select="true"
                                data-show-pagination-switch="true"
                                data-pagination="true"
                                data-id-field="id"
                                data-page-list="[10, 25, 50, 100, all]"
                                data-show-footer="true"
                                data-show-export="true"
                                class="table-striped"
                                data-detail-formatter="detailFormatter">
                            <thead>
                            <tr>
                                <th data-field="学号" data-align="center" data-sortable="true">学号</th>
                                <th data-field="姓名" data-align="center">姓名</th>
                                <th data-field="总分" data-sortable="true" data-align="center">总分</th>
                                <th data-field="门数" data-align="center">门数</th>
                                <th data-field="总学分" data-align="center">总学分</th>
                                <th data-field="获得学分" data-align="center">获得学分</th>
                                <th data-field="不及格学分" data-align="center">不及格<br>学分</th>
                                <th data-field="通过率" data-align="center">通过率</th>
                                <th data-field="算术平均分" data-align="center">算术<br>平均分</th>
                                <th data-field="排名1" data-sortable="true" data-align="center">排名1</th>
                                <th data-field="学分加权平均分" data-align="center" data-align="center">学分加<br>权平均分</th>
                                <th data-field="排名2" data-sortable="true" data-align="center">排名2</th>
                                <th data-field="平均学分绩点" data-align="center">平均学<br>分绩点</th>
                                <th data-field="排名3" data-sortable="true" data-align="center">排名3</th>
                                <th data-field="学分绩点和" data-align="center">学分<br>绩点和</th>
                                <th data-field="排名4" data-sortable="true" data-align="center">排名4</th>
                                <th data-field="不及格门数" data-sortable="true" data-align="center">不及格<br>门数</th>


                            </tr>
                            </thead>
                        </table>
                    </div>
                    <div role="tabpanel" class="tab-pane" id="profile">

                        <div id="toolbar2">

                        </div>
                        <table
                                id="table2"
                                data-detail-view="true"
                                data-show-toggle="true"
                                data-search="true"
                                data-toggle="table"
                                data-toolbar="#toolbar2"
                                data-show-columns="true"
                                data-click-to-select="true"
                                data-show-pagination-switch="true"
                                data-pagination="true"
                                data-id-field="id"
                                data-page-list="[10, 25, 50, 100, all]"
                                data-show-footer="true"
                                data-show-export="true"
                                class="table-striped">
                            <thead>
                            <tr>
                                <!--<th  ng-repeat="name in className" data-field="{{name}}" data-align="center" data-sortable="true">{{name}}</th>-->
                                <!--<th data-field="姓名" data-align="center">姓名</th>
                                <th data-field="总分" data-sortable="true" data-align="center">总分</th>
                                <th data-field="门数" data-align="center">门数</th>
                                <th data-field="总学分" data-align="center">总学分</th>
                                <th data-field="获得学分" data-align="center">获得学分</th>
                                <th data-field="不及格学分" data-align="center">不及格<br>学分</th>
                                <th data-field="通过率" data-align="center">通过率</th>
                                <th data-field="算术平均分" data-align="center">算术<br>平均分</th>
                                <th data-field="排名1" data-sortable="true" data-align="center">排名1</th>
                                <th data-field="学分加权平均分" data-align="center" data-align="center">学分加<br>权平均分</th>
                                <th data-field="排名2" data-sortable="true" data-align="center">排名2</th>
                                <th data-field="平均学分绩点" data-align="center">平均学<br>分绩点</th>
                                <th data-field="排名3" data-sortable="true" data-align="center">排名3</th>
                                <th data-field="学分绩点和" data-align="center">学分<br>绩点和</th>
                                <th data-field="排名4" data-sortable="true" data-align="center">排名4</th>
                                <th data-field="不及格门数" data-sortable="true" data-align="center">不及格<br>门数</th>-->


                            </tr>
                            </thead>
                        </table>

                    </div>
                </div>

            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-sm-2 col-sm-offset-10">
            <button class="btn btn-block btn-primary" ng-click="saveDatas();">保存</button>
        </div>
    </div>
</div>
<script src="../js/plugins/angularjs/angular.min.js"></script>
<script src="../js/plugins/xlsx/xlsx.core.min.js"></script>
<script>
    function responseHandler(res) {
        console.log('responseHandler',res);
        res.forEach(function (row, i) {
            row.index = i+1;
        });
        return res
    }

    function detailFormatter(index, row) {
        var html = []
        $.each(row, function (key, value) {
            html.push('<p><b>' + key + ':</b> ' + value + '</p>')
        })
        return html.join('')
    }
</script>
<script>
    var app = angular.module("myApp", []);


    app.controller("filemyController", function ($scope) {

        $scope.saveDatas=function(){
            var $table = $('#table');
            alert(JSON.stringify($table.bootstrapTable('getData')))
        }

        $scope.readMyFile=function(obj){
            $('#location').val($('#i-file').val());
            var files = obj.files;

            var fileReader = new FileReader();
             fileReader.onload = function(ev) {
                try {
                    var data = ev.target.result,
                        workbook = XLSX.read(data, {
                            type: 'binary'
                        }), // 以二进制流方式读取得到整份excel表格对象
                        datas = []; // 存储获取到的数据
                } catch (e) {
                    console.log('文件类型不正确');
                    return;
                }

                // 表格的表格范围，可用于判断表头是否数量是否正确
                var fromTo = '';
                // 遍历每张表读取
                for (var sheet in workbook.Sheets) {
                    if (workbook.Sheets.hasOwnProperty(sheet)) {
                        fromTo = workbook.Sheets[sheet]['!ref'];
                        console.log(fromTo);
                        datas = datas.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet]));
                        // break; // 如果只取第一张表，就取消注释这行
                    }
                }
                $scope.datas=datas;
                console.log(datas);


                 /***
                  *获得表头信息
                  */
                  var row=datas[0];
                  var rowname=[];
                  for(attr in row){
                      rowname.push(attr);
                  }
                  console.log(rowname);

                  var className=rowname;
                  className.splice(2,15);
                  console.log(className);
                  $scope.className=className;

                  var columns=[];
                  for(index in $scope.className){
                      var col={align: 'center',valign: 'middle',sortable:true,width:20};
                      col.field=$scope.className[index];
                      col.title=$scope.className[index];
                      columns.push(col);
                  }
                 console.log(columns);
                 $scope.$apply();//刷新数据

                /*****表格样式*****/
                var $table = $('#table');
                 var $table2 = $('#table2');
                var $button = $('#button');

                $table.bootstrapTable('load',$scope.datas );
                 //$table2.bootstrapTable('load',$scope.datas );
                 $table2.bootstrapTable("destroy").bootstrapTable({
                     //直接加载json数据
                     data:$scope.datas,
                     columns: columns,
                     search: true,
                     toolbar: '#toolbar2',
                     fixedColumns: true,
                     fixedNumber:+3
                 });

             };
            /*****表格样式*****/

            // 以二进制方式打开文件
            fileReader.readAsBinaryString(obj.files[0]);

        }
     });
</script>


</body>
</html>